package pl.edu.agh.intobl.factories;

import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;

import org.uncommons.watchmaker.framework.factories.AbstractCandidateFactory;

import pl.edu.agh.intobl.ProblemSolver;
import pl.edu.agh.intobl.Route;
import pl.edu.agh.intobl.RouteSet;

/**
 * Generates random starting population. Each individual is legitimate solution to a problem
 */
public class RouteSetFactory extends AbstractCandidateFactory<RouteSet> {

	@Override
	public RouteSet generateRandomCandidate(Random arg0) {
		List<Integer> demanders = new LinkedList<Integer>(ProblemSolver.demands.keySet());
		RouteSet routeSet = new RouteSet();
		while(demanders.size() > 0) {
			Route route = new Route();
			Collections.shuffle(demanders);
			Integer demanderNumber = demanders.get(0);
			while(demanders.size() > 0 && route.addDemander(demanderNumber, ProblemSolver.demands.get(demanderNumber))) {
				demanders.remove(demanderNumber);
				if (demanders.size() > 0)
					demanderNumber = demanders.get(0);
			}
				
			routeSet.addRoute(route);
		}
		
		return routeSet;
	}
}
